package android.slc.box.core.provider;

import org.jetbrains.annotations.NotNull;

import java.util.*;

public class BoxesResourceBundleWrap extends ResourceBundle {
    private final Locale locale;
    private final List<ResourceBundle> resourceBundleList = new ArrayList<>();

    public BoxesResourceBundleWrap(Locale locale, String... baseNames) {
        this.locale = locale;
        if (baseNames != null && baseNames.length > 0) {
            for (String baseName : baseNames) {
                this.resourceBundleList.add(ResourceBundle.getBundle(baseName, locale));
            }
        }
    }

    public void addResourceBundleByBaeName(String baseName) {
        this.resourceBundleList.add(ResourceBundle.getBundle(baseName, locale));
    }

    public void addResourceBundle(ResourceBundle resourceBundle) {
        this.resourceBundleList.add(resourceBundle);
    }

    @Override
    public Object handleGetObject(String key) {
        if (key == null) {
            throw new NullPointerException();
        }
        Object valueObj = null;
        for (ResourceBundle resourceBundle : resourceBundleList) {
            try {
                valueObj = resourceBundle.getObject(key);
                break;
            } catch (MissingResourceException ignored) {
            }
        }
        if (valueObj == null) {
            throw new MissingResourceException("Can't find resource for bundle "
                    + this.getClass().getName()
                    + ", key " + key,
                    this.getClass().getName(),
                    key);
        }
        return valueObj;
    }

    @NotNull
    @Override
    public Enumeration<String> getKeys() {
        return new ResourceBundleEnumeration(resourceBundleList);
    }

    public static class ResourceBundleEnumeration implements Enumeration<String> {
        private final List<Enumeration<String>> enumerations;

        public ResourceBundleEnumeration(List<ResourceBundle> resourceBundleList) {
            this.enumerations = new ArrayList<>();
            resourceBundleList.forEach(resourceBundle -> {
                this.enumerations.add(resourceBundle.getKeys());
            });
        }

        public ResourceBundleEnumeration(Enumeration<String>... enumerations) {
            this.enumerations = Arrays.asList(enumerations);
        }

        @Override
        public boolean hasMoreElements() {
            for (Enumeration<String> enumeration : enumerations) {
                if (enumeration.hasMoreElements()) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public String nextElement() {
            String next = null;
            for (Enumeration<String> enumeration : enumerations) {
                try {
                    next = enumeration.nextElement();
                    break;
                } catch (NoSuchElementException ignored) {
                }
            }
            if (next == null) {
                throw new NoSuchElementException();
            }
            return next;
        }
    }

}
